package com.jiuzhe.commercial.ui.my;

import android.content.Intent;

import com.jiuzhe.commercial.R;
import com.jiuzhe.commercial.base.BaseActivity;
import com.jiuzhe.commercial.constant.Constants;
import com.jiuzhe.commercial.http.CallBack;
import com.jiuzhe.commercial.http.HttpClient2;
import com.jiuzhe.commercial.utils.SPUtil;
import com.jiuzhe.commercial.utils.UIUtils;
import com.jungly.gridpasswordview.GridPasswordView;

import java.util.HashMap;
import java.util.List;

import butterknife.BindView;

/**
 * Created by huanghao on 2018/5/31.
 */

public class ResetTwoPayPassWorodActivity extends BaseActivity {
    @BindView(R.id.pswView)
    GridPasswordView pswView;
    private String newPwd;

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.activity_reset_psw);
        setTitle("设置提现密码");
        newPwd = getIntent().getStringExtra("newPwd");
    }

    @Override
    protected void loadData() {
        pswView.setOnPasswordChangedListener(new GridPasswordView.OnPasswordChangedListener() {
            @Override
            public void onTextChanged(String psw) {

            }

            @Override
            public void onInputFinish(String psw) {
                if (!psw.equals(newPwd)){
                    UIUtils.showToastSafe("两次输入的密码不同");
                    pswView.clearPassword();
                    return;
                }
                updateAccountPwd(psw);
            }
        });
    }

    private void updateAccountPwd(String psw) {
        HashMap<String, String> map = new HashMap<>();
        String mer_id = SPUtil.getString(this, Constants.MERCHANTID);
        String id = SPUtil.getString(this, Constants.ID);

        String mingwenNewPwd = psw + id.substring(3,10);
        String pwdNewMD5 = UIUtils.Md5(mingwenNewPwd);

        String token = SPUtil.getString(this, Constants.TOKEN);
        String autoSignStr = Constants.GETBACKPASSWD.substring(Constants.GETBACKPASSWD.lastIndexOf("0/")+1)  + "?token=";
        String signStr = UIUtils.Md5(autoSignStr + token);

        map.put("new_passwd",pwdNewMD5);
        map.put("user_id",mer_id);

        map.put("token",id);
        map.put("sign",signStr);

        HttpClient2.post(this, Constants.GETBACKPASSWD, map, new CallBack<List<String>>() {
            @Override
            public void onSuccess(List<String> stringList) {
                if(stringList.get(0).equals("30")){
                    UIUtils.showToastSafe("密码找回成功");
                }else {
                    UIUtils.showToastSafe("密码找回失败");
                }
                Intent intent  = new Intent(ResetTwoPayPassWorodActivity.this, PaySettingActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
        });
    }
}
